/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.component.analyzer.service.task;

import com.example.common.api.service.base.TaskHandler;
import com.example.common.spring.util.RestClient;
import com.example.common.util.CapDateTimeUtil;
import com.example.component.analyzer.common.constant.RefRestApiConstant;
import com.example.component.analyzer.common.dto.GoldResponseDTO;
import com.example.component.analyzer.core.model.Gold;
import com.example.component.analyzer.service.gold.GoldDataAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-07-03 23:56
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-07-03
 * @modify reason: {方法名}:{原因}
 * ...
 */
@EnableScheduling
@Slf4j
public class GoldScheduleTask implements TaskHandler {

    @Resource
    private GoldDataAnalysisService goldDataAnalysisService;

    /**
     * 任务执行体
     *  每10分钟执行一次,记录黄金价格
     */
    @Override
    @Scheduled(cron = "0 0/10 * * * ?")
    public void handle() {
        log.info("saveGoldPrice定时任务开始执行");
        GoldResponseDTO result = RestClient.getInstance().exchange(RefRestApiConstant.REST_GOLD_OPEN_API, HttpMethod.GET, GoldResponseDTO.class);
        Gold gold = new Gold();
        BeanUtils.copyProperties(result.getBody(), gold);
        gold.setCreateTime(CapDateTimeUtil.currentDate());
        goldDataAnalysisService.save(gold);
    }
}
